from library_main import LibraryApp
import tkinter as tk
from course_management.course_management import CourseManagement # 如果 main.py 在上一级目录
from tkinter import messagebox
import subprocess
import os

class MainApp:
    def __init__(self, root):
        self.root = root
        self.root.title("Main Application")
        self.root.geometry("800x600") 
        self.root.configure(bg="#f0f0f0")

        self.title_label = tk.Label(
            self.root,
            text="Welcome to the Dynamic Project",
            font=("Arial", 24, "bold"),
            bg="#f0f0f0",
            fg="#333333"
        )
        self.title_label.pack(pady=20)

        self.course_management_button = tk.Button(
            self.root,
            text="Course Management",
            command=self.show_course_management,  
            font=("Arial", 18, "bold"),
            bg="#4CAF50",  
            fg="white",    
            width=20,
            height=3,
            relief="flat",  
            activebackground="#45a049"  
        )
        self.course_management_button.pack(pady=10)
        self.course_management = None

        self.library_management_button = tk.Button(
            self.root,
            text="Library Management",
            command=self.show_library_management,  
            font=("Arial", 18, "bold"),
            bg="#4CAF50",  
            fg="white",    
            width=20,
            height=3,
            relief="flat",  
            activebackground="#45a049" 
        )
        self.library_management_button.pack(pady=10)

        # Social Management Button
        self.social_button = tk.Button(
            self.root,
            text="Social Management",
            command=self.launch_social_gui,
            font=("Arial", 18, "bold"),
            bg="#4CAF50",  
            fg="white",    
            width=20,
            height=3,
            relief="flat",  
            activebackground="#45a049"
        )
        self.social_button.pack(pady=10)

    def show_course_management(self):
        """显示 Course Management 界面"""
        if not self.course_management:
            # 隐藏主界面
            self.title_label.pack_forget()
            self.course_management_button.pack_forget()

            # 初始化 CourseManagement 界面
            self.course_management = CourseManagement(self.root)
        else:
            # 如果已经初始化，则直接显示
            self.course_management.show_management_widgets()
    
        print("Course Management")

    def show_library_management(self):
        root = tk.Toplevel(self.root)
        app = LibraryApp(root)
        root.mainloop()

    def launch_social_gui(self):
        try:
            # 获取当前脚本所在的目录
            script_dir = os.path.dirname(os.path.abspath(__file__))
            # 构建 gui.py 的完整路径
            gui_script_path = os.path.join(script_dir, 'social_system', 'gui.py')
            # 启动 gui.py 作为一个独立的进程
            subprocess.Popen(['python', gui_script_path])
        except Exception as e:
            messagebox.showerror("Error", f"Failed to launch social management GUI: {str(e)}")

if __name__ == "__main__":
    root = tk.Tk()
    app = MainApp(root)
    root.mainloop()